import React, { useEffect } from 'react'
import Router from '../src/router/index'
import '../src/styles/layout.css'
import { message, notification } from 'antd'
import { getUserData, getCurrentSummonerRankInfo } from './utils/service'

const App: React.FC = () => {
  const [api, contextHolder] = notification.useNotification()

  useEffect(() => {
    console.log("作者：云散，联系方式：@qq，@163");
    console.log("@qq：nomal.si2621.vip@qq.com");
    console.log("@163：qwe1943478251@163.com");
    

    getSummonerData().catch((error: any) => {
      console.error('[本机账号信息]: API未成功执行(可能未启动LOL客户端):', error.code, error.message)
    })
    getCurrentSummonerRankInfo()
      .then((res: any) => {
        if (res && res.data) {
          localStorage.setItem('currentSummonerRankInfo', JSON.stringify(res.data.highestRankedEntrySR))
        }
      })
      .catch((error: any) => {
        console.error('[本地召唤师rank]: API未成功执行(可能未启动LOL客户端):', error.code, error.message)
      })
  }, [])

  const getSummonerData = async () => {
    const res = await getUserData()
    try {
      if (res && res.data) {
        localStorage.setItem('playerData', JSON.stringify(res.data))
        localStorage.setItem('puuid', JSON.stringify(res.data.puuid))
        localStorage.setItem('init_name', JSON.stringify(res.data.displayName))
        localStorage.setItem('init_data', JSON.stringify(res.data))
      } else {
        initError()
      }
    } catch (error: any) {
      initError()
    }
  }

  const initError = () => {
    api['error']({
      message: '[本机账号信息]',
      description: 'API未成功执行(可能未启动LOL客户端或不是Admin权限).',
      duration: 0
    })
  }

  return (
    <div className="App">
      {contextHolder}
      <Router />
    </div>
  )
}

export default App
